package com_fit.springboot_fit.controller;

import com_fit.springboot_fit.service.AdminPowerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/power")
public class PowerController {

    @Autowired
    private AdminPowerService powerService;

    /**
     * 给用户分配权限
     */
    @PostMapping("/assignUserPermissions")
    public Map<String, Object> assignUserPermissions(
            @RequestParam Integer uid,
            @RequestParam List<Integer> menuIds) {

        Map<String, Object> result = new HashMap<>();

        try {
            powerService.assignUserPermissions(uid, menuIds);
            result.put("code", 200);
            result.put("message", "权限分配成功");

        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", e.getMessage());
        }

        return result;
    }

    /**
     * 给用户组分配权限
     */
    @PostMapping("/assignGroupPermissions")
    public Map<String, Object> assignGroupPermissions(
            @RequestParam Integer gid,
            @RequestParam List<Integer> menuIds) {

        Map<String, Object> result = new HashMap<>();

        try {
            powerService.assignGroupPermissions(gid, menuIds);
            result.put("code", 200);
            result.put("message", "权限分配成功");

        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", e.getMessage());
        }

        return result;
    }
}
